<template>
    <div>
        <div v-if="hackReset && data.length" class="list-item" v-for="(item, index) in data" :key="index">
            <template v-if="item !=null && item !='' ">
                <span class="title-txt">{{item}}</span>
                <div class="list-item-cover">
                    <Icon type="ios-eye-outline" @click.native="handleView(index,item)"></Icon>
                    <Icon type="ios-trash-outline" @click.native="handleRemove(index)"></Icon>
                </div>
            </template>
        </div>
        <div>
            <Button type="primary" @click.native="handleAdd">添加标签</Button>
        </div>
        <Modal v-model="visible">
            <p slot="header">
				<span>{{title}}</span>
			</p>
            <div class="body">
                <Input v-model="valueTxt" placeholder="请输入标签" :maxlength="16"/>
            </div>
            <div slot="footer">
                <Button
                    type="primary"
                    size="large"
                    @click="handleAction()">
                    {{actionType == 1 ? '添加' : '编辑'}}
                </Button>
            </div>
        </Modal>
        
    </div>
</template>

<script>
export default {
    props: ['data'],
    data() {
        return {
            hackReset: true,
            visible: false,
            actionType: 1,
            title: '添加',
            valueTxt:'',
            dataPos:null
        }
    },
    methods: {
        handleView (index,name) {
            this.actionType = 2;
            this.title = '编辑';
            this.visible = true;
            this.valueTxt = name;
            this.dataPos = index;
        },
        handleRemove (index) {
            this.data.splice(index,1);
            //this.$emit('on-action-remove', item);
        },
        handleAdd(){
            this.actionType = 1;
            this.title = '添加';
            this.visible = true;
        },
        handleAction(){
            if(this.valueTxt==null || this.valueTxt.trim == ''){
                return;
            }
            if(this.actionType==1){ // 添加
                this.data.push(this.valueTxt);
            }else if(this.actionType==2) { //编辑
                this.data[this.dataPos] = this.valueTxt;
            }
            this.actionType=null;
            this.valueTxt='';
            this.visible = false;
        }
    },
    watch: {
        data(val,old){
            // vue强制刷新组件
            this.hackReset = false
            this.$nextTick(() => {
                this.hackReset = true
            });
        }
    }
}
</script>

<style scoped>
.list-item{
    display: inline-block;
    height: 30px;
    text-align: center;
    line-height: 30px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #2d8cf0;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
    padding-left: 10px;
    padding-right: 10px;
}
.list-item .title-txt{
    color: #fff;
}

.add-btn{
    display: inline-block;
}

.list-item-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
}
.list-item:hover .list-item-cover{
    display: block;
}
.list-item-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
}
</style>